
from fastapi import HTTPException

class HttpCode():
    def __init__(self, code, name):
        self.code = code
        self.name = name
    def getCode(self):
        return self.code
    def getName(self):
        return self.name

class HttpResponse:
    """HTTP响应结果
    """
    SUCCESS = HttpCode(200, '成功')
    FAILED  = HttpCode(0, '失败')
    UNAUTHORIZED = HttpCode(401, '访问资源的权限不够')
    FORBIDDEN = HttpCode(403, '没有权限访问资源')
    NOT_FOUND = HttpCode(404, '需要访问的资源不存在')
    SYSTEM_ERROR = HttpCode(500, '系统错误')
    GATEWAY_TIMEOUT = HttpCode(504, '请求超时')

def error(msg="", data=None):
    raise HTTPException(status_code=HttpResponse.SYSTEM_ERROR.code, detail=msg)

def success(msg="操作成功", data=None):
    """基础返回格式"""
    if data is None:
        data = []
    result = {
        "code": HttpResponse.SUCCESS.code,
        "msg": msg,
        "data": data
    }
    return result